﻿layui.use(['form', 'table', 'http', 'popup', 'upload', 'global'], function () {
    var form = layui.form;
    var table = layui.table;
    var $ = layui.$;
    var popup = layui.popup;
    var http = layui.http;
    var upload = layui.upload;
    var global = layui.global;

    form.on('select(LibraryId)', function (data) {
        http.ajax({
            url: '/Book/GetBookshelf',
            type: 'get',
            data: { libraryId: data.value },
            success: function (res) {
                if (res) {
                    var html = '';
                    $.each(res, function (i, item) {
                        html += ' <option value="' + item.value + '">' + item.text + '</option>';
                    })
                    $("#bookselfId").html(html);
                    form.render('select');
                }
            }
        });
    });

    upload.render({
        elem: '#uploadCover',
        url: '/Upload/UploadImage',
        accept: global.image.accept,
        acceptMime: global.image.imageAcceptMime,
        exts: global.image.imageExts,
        choose: function (obj) {
            obj.preview(function (index, file, result) {
                $("#coverShow").removeClass("layui-hide").find("img").attr("src", result);
            });
        },
        before: function () {
            layer.load(2);
        },
        done: function (res) {
            layer.closeAll('loading');
            if (res.code === 0) {
                $("#cover").val(res.data.url);
            } else {
                popup.toastError(res.msg);
            }
        },
        error: function () {
            layer.closeAll('loading');
            popup.toastError("上传失败，请稍后重试");
        }
    });
    upload.render({
        elem: '#pdf',
        url: '/Upload/UploadPdf',
        accept: "flie",
        acceptMime: "application/pdf",
        exts: "pdf",
        choose: function (obj) {
            obj.preview(function (index, file, result) {
                $("#pdf").val(file.name);
            });
        },
        before: function () {
            layer.load(2);
        },
        done: function (res) {
            layer.closeAll('loading');
            if (res.code !== 0) {
                popup.toastError(res.msg);
            }
        },
        error: function () {
            layer.closeAll('loading');
            popup.toastError("上传失败，请稍后重试");
        }
    });



});